#!/bin/bash
#
# Redis - this script starts and stops the redis server daemon
#
# chkconfig: - 85 15
# description: Redis is an open source, BSD licensed, advanced key-value cache and store.
#              It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs.
# processname: Redis Server
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/redisd ]; then
    . /etc/sysconfig/redisd
fi

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

REDIS_HOME="/usr/local/Redis"
redisd=$REDIS_HOME"/bin/redis-server"
PROG="Redis Server"
configfile=${REDIS_HOME}"/etc/redis.conf"
pidfile=${PIDFILE-/var/run/redis.pid}
lockfile=${LOCKFILE-/var/lock/subsys/redis}

RETVAL=0

start() {
    [ -x $redisd ] || exit 5
    [ -f $configfile ] || exit 6
    echo -n $"Starting $PROG: "
    daemon $redisd $configfile
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $lockfile
    return $RETVAL
}

stop() {
    echo -n $"Stopping $PROG: "
    killproc $redisd -QUIT
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

case "$1" in
    start)
        start
        ;;
    reload|restart)
        stop
        start
        ;;
    stop)
        stop
        ;;
    condrestart|try-restart)
        if status >&/dev/null; then
            stop
            start
        fi
        ;;
    status)
        status -p ${pidfile} $redisd
        RETVAL=$?
        ;;
    help)
        $redisd --help
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $PROG {start|reload|restart|condrestart|try-restart|stop|status|help}"
        RETVAL=2
        ;;
esac

exit $RETVAL
